package com.sinosoft.dsp.system.to.attribute;

import com.sinosoft.dsp.system.to.exception.SinoAttributeException;
import com.sinosoft.dsp.system.to.exception.SinoAttributeValidationException;
import com.sinosoft.dsp.system.to.helper.CommonAttributeType;
import com.sinosoft.dsp.system.to.helper.ToHelper;

/**
 * 属性类型抽象类
 * 
 * @since 1.0 2010-4-22
 * @author 潘巍（Peter Pan）
 * 
 * @param <T>值类型
 */
public abstract class AbstractType<T> {

	public static final String XML_META_TAG = "value-meta";
	public static final String XML_DATA_TAG = "value-data";

	private String id;
	private String name;
	private int length;
	private int precision;
	private int sortType;
	private int trimType;
	private String origin;
	private String comments;
	private T value = null;
	private String concept;
	private boolean sortedDescending = false;

	public boolean isSortedDescending() {
		return sortedDescending;
	}

	public void setSortedDescending(boolean sortedDescending) {
		this.sortedDescending = sortedDescending;
	}

	public void setConcept(String concept) {
		this.concept = concept;
	}

	public String getConcept() {
		return this.concept;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public void setLength(int length, int precision) {
		this.length = length;
		this.precision = precision;
	}

	public int getPrecision() {
		return precision;
	}

	public void setPrecision(int precision) {
		this.precision = precision;
	}

	public String getOrigin() {
		return origin;
	}

	public void setOrigin(String origin) {
		this.origin = origin;
	}

	public String getComments() {
		return comments;
	}

	public void setComments(String comments) {
		this.comments = comments;
	}

	/**
	 * 排序类型
	 * 
	 * @param type
	 * @throws SinoAttributeException
	 */
	public void setSortType(int type) throws SinoAttributeException {
		if (0 <= type && type < Attribute.sortTypeCode.length) {
			this.sortType = type;
		} else {
			throw new SinoAttributeException("设置排序类型发生数组越界");
		}

	}

	public int getSortType() {
		return this.sortType;
	}

	public String getSortTypeCode() {
		return Attribute.sortTypeCode[this.sortType];
	}

	public String getSortTypeDesc() {
		return Attribute.sortTypeDesc[this.sortType];
	}

	public void setTrimType(int type) throws SinoAttributeException {
		if (0 <= type && type < Attribute.trimTypeCode.length) {
			this.trimType = type;
		} else {
			throw new SinoAttributeException("设置剪切类型发生数组越界");
		}
	}

	public int getTrimType() {
		return this.trimType;
	}

	public String getTrimTypeCode() {
		return Attribute.trimTypeCode[this.trimType];
	}

	public String getTrimTypeDesc() {
		return Attribute.trimTypeDesc[this.trimType];
	}

	public void validate() throws SinoAttributeValidationException {

	}

	public T getValue() {
		return value;
	}

	public void setValue(T t) {
		this.origin = t.toString();
		this.value = t;
	}

	public String getId() {
		return this.id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@SuppressWarnings("unchecked")
	public Attribute<T> cloneAttribute() throws SinoAttributeException {
		Attribute<T> attr = (Attribute<T>) ToHelper.createToAttribute(this
				.getType());
		return cloneCommonAttribute(attr);
	}

	private Attribute<T> cloneCommonAttribute(Attribute<T> attr)
			throws SinoAttributeException {
		attr.setId(this.id);
		attr.setName(this.name);
		attr.setLength(this.length);
		attr.setPrecision(this.precision);
		attr.setSortType(this.sortType);
		attr.setTrimType(this.trimType);
		attr.setOrigin(this.origin);
		attr.setComments(this.comments);
		attr.instantiateFromObject(this.value);
		return attr;
	}

	public boolean isNull() throws SinoAttributeException {
		if (this.value == null) {
			return true;
		}
		return false;
	}

	public abstract CommonAttributeType getType();

}
